package com.bw.aop.config;

import cn.hutool.jwt.JWT;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.aop.annotation.PhoneValidator;


import com.ruoyi.common.core.domain.R;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@Aspect
public class UserAop {
    @Pointcut("@annotation(com.bw.aop.annotation.PhoneValidator)")
    public void pt() {

    }
    @Autowired
    private HttpServletRequest request;

    @Autowired
    RedisTemplate redisTemplate;
    @Around("pt()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取用户
        String token = request.getHeader("token");
        JWT of = JWT.of(token);

        // 获取目标方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        // 获取方法上的权限注解
        PhoneValidator permission = method.getAnnotation(PhoneValidator.class);
        if (permission != null) {
            String role = (String) of.getPayload("role");
            System.out.println("-------------角色是"+role);
            if (!role.equals(permission.value())) {
                // 权限不足，抛出异常
                return R.fail("权限不足");
            }
        }
        return joinPoint.proceed();
    }




}
